package com.example.administrator.baikangxing.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.administrator.baikangxing.R;


public class CustomView extends View {

    private int progressColor;
    private int baseColor;

    private RectF mBounds;
    private Paint mPaint;
    private int mBorderWidth = 2;
    private float width;
    private float height;
    private float radius;
    private float smallLength;
    private float largeLength;

    private Paint basePaint;
    private Paint progressPaint;
    private int baseWidth = 18;
    private int progressWidth = 23;

    private int progress = 0;
    public CustomView(Context context) {
        super(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView,0,0);

        try{
            progressColor = typedArray.getColor(R.styleable.CustomView_l_progress_color,0xff0000);
            baseColor = typedArray.getColor(R.styleable.CustomView_l_base_color,0x00ff00);
            baseWidth = typedArray.getDimensionPixelSize(R.styleable.CustomView_l_base_width,18);
            progressWidth = typedArray.getDimensionPixelSize(R.styleable.CustomView_l_progress_width,23);
        }finally {
            typedArray.recycle();
        }
        init();
    }

    public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mBorderWidth);
        mPaint.setColor(baseColor);

        basePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        basePaint.setStyle(Paint.Style.STROKE);
        basePaint.setStrokeWidth(baseWidth);
        basePaint.setColor(baseColor);
        basePaint.setAntiAlias(true);
        basePaint.setDither(true);
        basePaint.setStrokeJoin(Paint.Join.ROUND);

        progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeWidth(progressWidth);
        progressPaint.setColor(progressColor);
        progressPaint.setAntiAlias(true);
        progressPaint.setStrokeJoin(Paint.Join.ROUND);
        progressPaint.setDither(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mBounds = new RectF(getLeft(),getTop(),getRight(),getBottom());
        width = mBounds.right - mBounds.left;
        height = mBounds.bottom - mBounds.top;
        if (width < height){
            radius = width / 4;
        }else {
            radius = height / 4;
        }
        smallLength = 10;
        largeLength = 20;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(0xffffff);
        float left = mBounds.centerX()- radius;
        float top = mBounds.centerY() - radius;
        float right = mBounds.centerX()+ radius;
        float bottom = mBounds.centerY() + radius;
        Log.e("lym","my size==="+left+":"+top+":"+right+":"+bottom);
        progressPaint.setStrokeCap(Paint.Cap.BUTT);
        RectF rectF = new RectF(left,top,right,bottom);
        canvas.drawCircle(rectF.centerX(),rectF.centerY(),radius,basePaint);
        canvas.drawArc(rectF,270,progress,false,progressPaint);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        if (progress > 20)
            canvas.drawArc(rectF,280,progress+5,false,progressPaint);
    }

    public void setProgress(int progress){
        this.progress = progress;
        invalidate();
    }
}
